package com.shigen.file.util;

import java.text.DecimalFormat;

/**
 * 文件工具类
 *
 * @author shigenfu
 * @date 2022/6/15 8:58 下午
 */
public class FileUtil {

  /**
   * 根据文件大小格式化
   */
  public static String formatFileSize(Long fileSize) {
    String[] arr = {"Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
    int index = (int) (Math.floor(Math.log(fileSize) / Math.log(1024)));
    double size = fileSize / Math.pow(1024, index);
    size = Double.parseDouble(new DecimalFormat("#.00").format(size));
    return size + arr[index];
  }

}
